<?php
require_once ('config.php');
require_once ('databasefunctions.php');
require_once ('displayfunctions.php');
validate_user();
html_header();

//Show menu

switch ($_GET['action']) {
        case "add_form" :
                title_bar($langchargetypemaster);
                ?>
                <br><? $langoperation . " - " . $langadd ?><br>
                <form method = "GET" action="chargetypemaster.php">
                <table>
                        <tr bgcolor='<? echo $color1 ?>'>
                                <td><? echo $langchargeid ?>
                                <td><? echo $langchargetype ?>
                                <td><? echo $langchargedescription ?>
                                <td><? echo $langenablestatus ?>
                        <tr>
                                <td><input type = 'text' name = 'ChargeID' maxlength = '10'>
                                <td><select name = 'ChargeType'>
                                                <option value='H'><? echo $langhourlycharge ?>
                                                <option value='E'><? echo $langexpense ?>
                                                <option value='F'><? echo $langexpensefl ?>
                                        </select>
                                <td><input type = 'text' name = 'ChargeDescription' maxlength = '50'>
                                <td><select name = 'Enable'>
                                                <option value='0'><? echo $langdisable ?>
                                                <option value='1'><? echo $langenable ?>
                                        </select>
                                        <input type = 'hidden' name = 'action' value = 'add'>
                                <td><input type = 'submit' value = '<? echo $langadd ?>'>
                </table>
                </form>
                <?
                break;

        case "add" :
                if (empty($_GET['ChargeID'])) {
                        message_box($langchargeid . " " . $langcannotempty);
                        ?>
                                <script>history.go(-1);</script>
                        <?
                        exit;
                }
                $sql = "INSERT INTO chargetypemaster (ChargeID, ChargeType, ChargeDescription, Enable) values (";
                $sql .= "'" . strtoupper($_GET['ChargeID']) . "' ,";
                $sql .= "'" . $_GET['ChargeType'] . "' ,";
                $sql .= "'" . $_GET['ChargeDescription'] . "', ";
                $sql .= "'" . $_GET['Enable'] . "'";
                $sql .= ")";

                $sql_result = sql_execute($sql);
                echo "<br>$langchargeid $langadded<br>";
                title_bar($langchargetypemaster, 1);
                break;

        case "modify_form" :
                title_bar($langchargetypemaster, 1);
                // Obtain values from table
                $sql = "SELECT * FROM chargetypemaster WHERE ChargeID='" . $_GET['ChargeID']. "'";
                $sql_result = sql_execute($sql);
                $sql_data = mysql_fetch_array($sql_result);

                ?>
                <br><? echo $langoperation . " - " . $langmodify ?><br>
                <form method = "GET" action="chargetypemaster.php">
                <table>
                        <tr bgcolor='<? echo $color1 ?>'>
                                <td><? echo $langchargeid ?>
                                <td><? echo $langchargetype ?>
                                <td><? echo $langchargedescription ?>
                                <td><? echo $langenable ?>
                        <tr>
                                <td><input type = 'text' name = 'ChargeID' value = '<? echo $_GET['ChargeID'] ?>'
                                        maxlength = '10' readonly>
                                <td><select name = 'ChargeType'>
                                        <?
                                        if ($sql_data['ChargeType'] == 'H') {
                                                echo "<option value='E'>$langexpense";
                                                echo "<option value='H' SELECTED>$langhourlycharge";
                                                echo "<option value='F'>$langexpensefl";
                                        }
                                        else if ($sql_data['ChargeType'] == 'H') {
                                                echo "<option value='E' SELECTED>$langexpense";
                                                echo "<option value='H'>$langhourlycharge";
                                                echo "<option value='F'>$langexpensefl";
                                        }
                                        else {
                                                echo "<option value='E'>$langexpense";
                                                echo "<option value='H'>$langhourlycharge";
                                                echo "<option value='F' SELECTED>$langexpensefl";
                                        }
                                        ?>
                                        </select>
                                <td><input type = 'text' name = 'ChargeDescription' maxlength = '50' value='<?
                                                echo $sql_data['ChargeDescription'] ?> '>
                                <td><select name = 'Enable'>
                                        <?
                                        if ($sql_data['Enable'] == '0') {
                                                echo "<option value='0' SELECTED>$langdisable";
                                                echo "<option value='1'>$langenable";
                                        }
                                        else {
                                                echo "<option value='0'>$langdisable";
                                                echo "<option value='1' SELECTED>$langenable";
                                        }
                                        ?>
                                        </select>
                                        <input type = 'hidden' name = 'action' value = 'modify'>
                                <td><input type = 'submit' value = '<? echo $langmodify ?>'>
                </table>

                </form>
                <?
                break;

        case "modify" :
                $sql = "UPDATE chargetypemaster SET ";
                $sql .= "ChargeDescription = '" . $_GET['ChargeDescription'] . "', ";
                $sql .= "ChargeType = '" . $_GET['ChargeType'] . "', ";
                $sql .= "Enable = '" . $_GET['Enable'] . "' ";
                $sql .= "WHERE ChargeID = '" . $_GET['ChargeID'] . "'";

                $sql_result = sql_execute($sql);
                echo "<br>$langchargetype $langmodified<br>";
                title_bar($langchargetypemaster, 1);
                break;

        case "delete" :
                // test existence of ChargeID in billtransaction table
                $sql = "SELECT ChargeID FROM billtransaction WHERE ChargeID = '" . $_GET['ChargeID'] ."' LIMIT 1";
                $sql_result = sql_execute($sql);
                $sql_data = mysql_fetch_array($sql_result);
                if ($sql_data[0]) {
                        $message .= '<br>' . $langtransactionexist . '<br>' . $langcannotdelcharge . '<br>';
                }

                // test existence of ChargeID in staffmaster table
                $sql = "SELECT StaffID FROM staffmaster WHERE StaffGrade = '" . $_GET['ChargeID'] ."'";
                $sql_result = sql_execute($sql);

                while ($sql_data = mysql_fetch_array($sql_result)) {
                        $users .= $sql_data['StaffID'] . " ";
                }

                if ($users) {
                        $message .= $users . " " . $langstaffhaschargeid . "<br>" . $langcannotdelcharge . "<br>";
                }

                if ($message) {
                        message_box($message);
                        ?>
                                <script>history.go(-1);</script>
                        <?
                        exit;
                }

                $sql = "DELETE FROM chargetypemaster WHERE ChargeID = '" . $_GET['ChargeID'] . "'";

                $sql_result = sql_execute($sql);
                echo "<br>$langchargeid $langdeleted<br>";
                title_bar($langchargetypemaster, 1);
                break;

        case "count" :
                save_search_list();
                $url_text = retrieve_search_list();
                $sql = select_statement('chargetypemaster');
                $sql_result = sql_execute($sql);
                $sql_data = mysql_fetch_array($sql_result);
                $no_of_record = $sql_data[0];

                ?>
                <script>
                this.location = 'chargetypemaster.php?action=browse&total_record=<? echo $no_of_record ?>
                                                &record_offset=0<? echo $url_text ?>'
                </script>
                <?
                break;

        case "browse" :
                navigation_bar('CHARGE TYPE MASTER',1);
                column_headings(array($langchargeid, $langchargetype, $langchargedescription, $langenablestatus));

                // List Charges here
                $sql = select_statement('chargetypemaster',1);
                $sql.= " ORDER BY ChargeID LIMIT " . $_GET['record_offset'] . ", " . $_SESSION['RecordPerPage'];
                $sql_result = sql_execute($sql);

                while ($sql_data = mysql_fetch_array($sql_result)) {
                        $rowcolor = row_color_change();
                        echo "\n<tr bgcolor='$rowcolor'>";
                        echo "<td>";
                        echo "<a href = 'chargetypemaster.php?action=modify_form&ChargeID=".$sql_data['ChargeID'];
                        echo "'>".$sql_data['ChargeID']."</a>";
                        echo "<td>";
                        if ($sql_data['ChargeType'] == 'H') echo $langhourlycharge;
                        else if ($sql_data['ChargeType'] == 'E') echo $langexpense;
                        else echo $langexpensefl;

                        echo "<td>";
                        echo $sql_data['ChargeDescription'];
                        echo "<td>";
                        if ($sql_data['Enable'] == 1) echo $langyes;
                        else echo $langno;
                        echo "<td>";
                        echo "<a href = 'chargetypemaster.php?action=delete&ChargeID=" .
                                $sql_data['ChargeID'] ."'>$langdelete</a>";
                }
                echo "</table>";
                break;

        default :
                title_bar($langchargetypemaster, 1);
                $field[] = array('title'=>$langchargeid, 'field'=>'ChargeID', 'length'=>10);
                $field[] = array('title'=>$langchargetype, 'field'=>'ChargeType', 'length'=>10);
                search_form($field, $_SERVER['PHP_SELF']);
                break;
}

html_footer();
?>
